package PAT;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class PPNode{
    int id;
    int sum=0;

    public PPNode(int id, int sum) {
        this.id = id;
        this.sum = sum;
    }
}

public class PAT1075 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();
        scanner.nextLine();
        scanner.nextLine();
        int[][] values = new int[n + 1][m + 1];
        for (int[] value : values) {
            Arrays.fill(value, -2);
        }
        while (k-->0){
            int i = scanner.nextInt();
            int j = scanner.nextInt();
            values[i][j] = Math.max(values[i][j],scanner.nextInt()) ;
        }
        PPNode[] sums = new PPNode[n + 1];
        for (int i = 0; i < sums.length; i++) {
            sums[i] = new PPNode(i,Arrays.stream(values[i]).filter(s->s!=-2 && s!=-1).sum());
        }
        Arrays.sort(sums, new Comparator<PPNode>() {
            @Override
            public int compare(PPNode o1, PPNode o2) {
                return o2.sum - o1.sum;
            }
        });
        int index =0;
        int temp = 0;
        int t2=1;
        for (PPNode sum : sums) {
            if(sum.sum == 0) break;
            if(temp != sum.sum) {
                index+=t2;
                t2 = 1;
            }
            else t2++;
            temp = sum.sum;
            System.out.printf("%d %05d %d", index,sum.id,sum.sum);
            for (int i = 1; i < values[sum.id].length; i++) {
                int pp = values[sum.id][i];
                if(pp==-2) System.out.print(" -");
                else System.out.printf(" %d",pp);
            }
            System.out.println("");
        }
    }
}
